﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;
using MaxStudio.Concrete.Component;
using System.Collections;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTTemperatureAndHumidity : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method


        private void BindGridView()
        {
            RPTTemperatureAndHumidityService RPTTemperatureAndHumidityServiceObj = ServiceFactory.getRPTTemperatureAndHumidityService();
            OperationResult<List<tb_RPT_TemperatureAndHumidity>> result = RPTTemperatureAndHumidityServiceObj.GetAllTemperatureAndHumidity();

            if (result.operationSuccess)
            {
                gvRPTTemperatureAndHumidityList.DataSource = result.obj;
                gvRPTTemperatureAndHumidityList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTTemperatureAndHumidityService RPTTemperatureAndHumidityServiceObj = ServiceFactory.getRPTTemperatureAndHumidityService();
            OperationResult<tb_RPT_TemperatureAndHumidity> resultEdit = RPTTemperatureAndHumidityServiceObj.GetTemperatureAndHumidityById(id);

            if (resultEdit.operationSuccess == true)
            {

                txtEditRecordDate.Text = resultEdit.obj.RecordDate;
                txtEditRecordTime.Text = resultEdit.obj.RecordTime;
                txtEditTemperature.Text = resultEdit.obj.Temperature;
                txtEditHumidity.Text = resultEdit.obj.Humidity;
                txtEditRecorder.Text = resultEdit.obj.Recorder;

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTTemperatureAndHumidityService RPTTemperatureAndHumidityServiceObj = ServiceFactory.getRPTTemperatureAndHumidityService();
            tb_RPT_TemperatureAndHumidity instance = new tb_RPT_TemperatureAndHumidity();

            instance.RecordDate = txtRecordDate.Text;
            instance.RecordTime = txtRecordTime.Text;
            instance.Temperature = txtTemperature.Text;
            instance.Humidity = txtHumidity.Text;
            instance.Recorder = txtRecorder.Text;

            OperationResult<tb_RPT_TemperatureAndHumidity> result = RPTTemperatureAndHumidityServiceObj.CreateTemperatureAndHumidity(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }
            RPTTemperatureAndHumidityService RPTTemperatureAndHumidityServiceObj = ServiceFactory.getRPTTemperatureAndHumidityService();
            tb_RPT_TemperatureAndHumidity entity = new tb_RPT_TemperatureAndHumidity();

            entity.Id = editId;
            entity.RecordDate = txtEditRecordDate.Text;
            entity.RecordTime = txtEditRecordTime.Text;
            entity.Temperature = txtEditTemperature.Text;
            entity.Humidity = txtEditHumidity.Text;
            entity.Recorder = txtEditRecorder.Text; 

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_TemperatureAndHumidity> resultEdit = RPTTemperatureAndHumidityServiceObj.ModifyTemperatureAndHumidity(entity);
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTTemperatureAndHumidityService RPTTemperatureAndHumidityServiceObj = ServiceFactory.getRPTTemperatureAndHumidityService();
            tb_RPT_TemperatureAndHumidity deleteEntity = new tb_RPT_TemperatureAndHumidity();
            deleteEntity.Id = deleteId;
            RPTTemperatureAndHumidityServiceObj.DeleteTemperatureAndHumidity(deleteEntity);

            BindGridView();
        }

        protected void gvRPTTemperatureAndHumidityList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvRPTTemperatureAndHumidityList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRPTTemperatureAndHumidityList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
